package handler

import (
	"ebip/internel/types"
	"ebip/pkg/ginl/wrap_gin"
	"github.com/gin-gonic/gin"
	"path"
)

// @Description
// @Author 代码小学生王木木

type FileHandler struct {
	types.Res
}

func NewFileHandler() *FileHandler {
	return &FileHandler{Res: types.Res{}}
}

func (f *FileHandler) Upload(c *gin.Context) (wrap_gin.Result, error) {
	file, err := c.FormFile("file")
	if err == nil {
		dst := path.Join("./static", file.Filename)
		saveErr := c.SaveUploadedFile(file, dst)
		if saveErr == nil {
			res := types.FileUploadResponse{
				FileName: file.Filename,
				FilePath: dst,
			}
			return f.SuccesJson(res), nil
		}
		return f.ErrX(800), saveErr
	}
	return f.ErrX(800), err
}
